package com.sita.tboard.hitchhike.pending;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.bike.support.GlobalContext;
import com.sita.tboard.hitchhike.bean.TicketModel;
import com.sita.tboard.hitchhike.detail.DriverTripDetailActivity;
import com.sita.tboard.util.DateUtils;

import java.util.ArrayList;

public class DriverPendingTicketsAdapter extends RecyclerView.Adapter<DriverPendingTicketsAdapter.PendingViewHolder> {

    private ArrayList<TicketModel> mValues;
    private Context mContext;

    public DriverPendingTicketsAdapter(Context context) {
        mContext = context;
        mValues = new ArrayList<>();
    }

    @Override
    public PendingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_hitchhike_driver_pending, parent, false);

        PendingViewHolder viewHolder = new PendingViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(PendingViewHolder holder, int position) {
        TicketModel item = mValues.get(position);
        holder.setItem(item);

    }

    public void setItems(ArrayList<TicketModel> items) {
        mValues = items;
        notifyDataSetChanged();
    }

    public TicketModel getItem(int position){
        return mValues.get(position);
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public void remove(int position) {
        mValues.remove(position);

        notifyItemRemoved(position);
    }

    public void insert(TicketModel item, int position) {
        mValues.add(position, item);
        notifyItemInserted(position);
    }

    public void append(ArrayList<TicketModel> items) {
        int count = getItemCount();
        mValues.addAll(count, items);
        if (items.size() == 1) {
            notifyItemInserted(count);
        } else if (items.size() > 1) {
            notifyItemRangeInserted(count, items.size());
        }
    }

    public class PendingViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private TicketModel mItem;

        private TextView mTimeView;
        private TextView mFromView;
        private TextView mToView;
        private LinearLayout mActionView;

        public PendingViewHolder(View view) {
            super(view);

            mTimeView = (TextView) view.findViewById(R.id.time);
            mFromView = (TextView) view.findViewById(R.id.from);
            mToView = (TextView) view.findViewById(R.id.to);
            mActionView = (LinearLayout) view.findViewById(R.id.btn_action);
            mActionView.setOnClickListener(this);
        }

        public void setItem(TicketModel item) {
            mItem = item;
            mTimeView.setText(GlobalContext.getGlobalContext().getString(R.string.departure_time, DateUtils.formatDateTime(mItem.orderDepartureTime)));
            mFromView.setText(GlobalContext.getGlobalContext().getString(R.string.from, mItem.fromaddress));
            mToView.setText(GlobalContext.getGlobalContext().getString(R.string.to, mItem.toaddress));
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_action:{
                    mContext.startActivity(DriverTripDetailActivity.newIntent(mItem));
                    break;
                }
            }
        }
    }

}
